package com.tiger.web.controller.bill;

import com.tiger.common.core.controller.BaseController;
import com.tiger.common.core.domain.AjaxResult;
import com.tiger.common.core.domain.model.LoginUser;
import com.tiger.common.core.page.TableDataInfo;
import com.tiger.common.utils.ServletUtils;
import com.tiger.common.utils.StringUtils;
import com.tiger.common.utils.spring.SpringUtils;
import com.tiger.framework.web.service.TokenService;
import com.tiger.project.domain.AssetReceipt;
import com.tiger.project.domain.AssetReceiptAllocation;
import com.tiger.project.domain.ContractSponsor;
import com.tiger.project.service.IAssetReceiptAllocationService;
import com.tiger.project.service.IAssetReceiptService;
import com.tiger.project.service.IContractSponsorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author bai
 * Date on 2022/10/9  15:41
 * Description:租金分配
 */
@Api(tags = "租金分配")
@RestController
@RequestMapping("/system/allocation")
public class AssetReceiptAllocationController extends BaseController {
    @Autowired
    private IAssetReceiptService assetReceiptService;
    @Autowired
    private IAssetReceiptAllocationService assetReceiptAllocationService;
    @Autowired
    private IContractSponsorService contractSponsorService;

    /**
     * 查询【租金分配】列表
     *
     */
    @ApiOperation(value = "查询【租金分配】列表")
    //@PreAuthorize("@ss.hasPermi('system:receipt:list')")
    @GetMapping("/list")
    public TableDataInfo list(ContractSponsor contractSponsor) {
        //合同发起审核通过后，就会有租金分配记录
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        contractSponsor.setCompanyId(loginUser.getUser().getDeptId());
        contractSponsor.setStatus(3L);
        startPage();
        List<ContractSponsor> list = contractSponsorService.selectContractSponsorAllocation(contractSponsor);
        return getDataTable(list);
    }

    /**
     * 获取【租金分配】详细信息
     */
    @ApiOperation(value = "获取【租金分配】列详细信息")
    //@PreAuthorize("@ss.hasPermi('system:receipt:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(contractSponsorService.selectContractSponsorAllocationById(id));
    }

    /**
     * 新增收款信息
     */
    @ApiOperation(value = "新增【收款信息】")
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody AssetReceiptAllocation assetReceiptAllocation){
        return toAjax(assetReceiptAllocationService.insertAssetReceiptAllocation(assetReceiptAllocation));
    }

    /**
     * 总未分配金额
     */
    @ApiOperation(value = "总分配金额")
    @GetMapping(value = "/noAllotMoney")
    public BigDecimal noAllotMoney(@ApiParam(value = "合同id") Long id){
        BigDecimal noAllotMoney = contractSponsorService.noAllotMoney(id);
        if (StringUtils.isNotNull(noAllotMoney)){
            return noAllotMoney;
        }else {
            return BigDecimal.valueOf(0);
        }
    }





}
